/*
 * Copyright 2012 Marsrutai.lt
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package lt.marsrutai.mobile.android;

import lt.marsrutai.mobile.android.cache.DataCache;
import lt.marsrutai.mobile.android.cache.DataCacheUpdater;
import lt.marsrutai.mobile.android.cache.relation.RelationDataCache;
import lt.marsrutai.mobile.android.dao.DataDao;
import lt.marsrutai.mobile.android.dao.OnlineJsonDataDao;
import lt.marsrutai.mobile.android.intercept.CacheInterceptor;
import lt.marsrutai.mobile.android.intercept.InterceptorProxy;
import lt.marsrutai.mobile.android.manager.CacheUpdateManager;
import lt.marsrutai.mobile.android.service.BookmarkService;
import lt.marsrutai.mobile.android.service.DataService;
import lt.marsrutai.mobile.android.service.LocationProvider;
import lt.marsrutai.mobile.android.service.ShapeManipulationService;
import lt.marsrutai.mobile.android.service.SupportBookmarkService;
import lt.marsrutai.mobile.android.service.TransportInfoResolver;
import lt.marsrutai.mobile.android.service.impl.DataServiceImpl;
import lt.marsrutai.mobile.android.service.impl.LocationProviderImpl;
import lt.marsrutai.mobile.android.service.impl.LocationProviderImpl.LocationUpdateRequest;
import lt.marsrutai.mobile.android.service.impl.ShapeManipulationServiceImpl;
import lt.marsrutai.mobile.android.service.impl.TransportInfoResolverImpl;
import android.content.Context;
import android.location.LocationManager;

public class AppContext {

    private static Context context;

    private static BookmarkService bookmarkService;
    private static DataService dataService;

    private static DataCache dataCache;
    private static CacheInterceptor cacheInterceptor;
    
    private static DataCacheUpdater dataCecheUpdater;
    private static CacheUpdateManager cacheUpdateManager;

    private static TransportInfoResolver transportInfoResolver;
    
    private static LocationProvider locationProvider;

    private static ShapeManipulationService shapeManipulationService;

    public static void init(Context ctx) {
        context = ctx;

        bookmarkService = new BookmarkService(context);

        //dataCache = new SerializeDataCache(context);
        dataCache = new RelationDataCache(context);
        cacheInterceptor = new CacheInterceptor(dataCache);
        DataDao dataDao = new OnlineJsonDataDao();

        DataDao api = (DataDao) InterceptorProxy.newInstance(dataDao, cacheInterceptor);
        dataService = new DataServiceImpl(api);

        dataCecheUpdater = new DataCacheUpdater(dataDao, dataCache);
        cacheUpdateManager = new CacheUpdateManager(dataCecheUpdater);
        
        shapeManipulationService = new ShapeManipulationServiceImpl();

        transportInfoResolver = new TransportInfoResolverImpl(context);
        ((TransportInfoResolverImpl) transportInfoResolver).setShapeManipulationService(shapeManipulationService);
        
        locationProvider = new LocationProviderImpl(context, new LocationUpdateRequest[] {
                new LocationUpdateRequest(LocationManager.NETWORK_PROVIDER, 60 * 1000, 0),
                new LocationUpdateRequest(LocationManager.GPS_PROVIDER, 60 * 1000, 0)});
        
        // migrate old bookmark format to a new one - FIXME remove this sometime in the future (added with 1.1.0)
        SupportBookmarkService supportBookmarService = new SupportBookmarkService(context);
        supportBookmarService.setBookmarkService(bookmarkService);
        supportBookmarService.migrate();
    }

    public static BookmarkService getBookmarkService() {
        return bookmarkService;
    }

    public static DataService getDataService() {
        return dataService;
    }

    public static CacheInterceptor getDataCacheController() {
        return cacheInterceptor;
    }

    public static CacheUpdateManager getCacheUpdateManager() {
        return cacheUpdateManager;
    }

    public static TransportInfoResolver getTransportInfoResolver() {
        return transportInfoResolver;
    }
    
    public static LocationProvider getLocationProvider() {
        return locationProvider;
    }

    public static DataCacheUpdater getDataCacheUpdater() {
        return dataCecheUpdater;
    }

    public static ShapeManipulationService getShapeManipulationService() {
        return shapeManipulationService;
    }
    
    public static Context getApplicationContext() {
        return context;
    }

}
